home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / fw_xemacs.idb / usr / freeware / lib / xemacs-20.4 / lisp / auctex / tex-buf.el.z / tex-buf.el
Encoding:
Text File  |  1998-05-21  |  58.1 KB  |  1,624 lines

  1. ;;; tex-buf.el - External commands for AUC TeX.
  2. ;;
  3. ;; Maintainer: Per Abrahamsen <auc-tex@sunsite.auc.dk>
  4. ;; Version: 9.7p
  5.  
  6. ;; Copyright (C) 1991 Kresten Krab Thorup
  7. ;; Copyright (C) 1993, 1996 Per Abrahamsen 
  8. ;; 
  9. ;; This program is free software; you can redistribute it and/or modify
  10. ;; it under the terms of the GNU General Public License as published by
  11. ;; the Free Software Foundation; either version 1, or (at your option)
  12. ;; any later version.
  13. ;; 
  14. ;; This program is distributed in the hope that it will be useful,
  15. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17. ;; GNU General Public License for more details.
  18. ;; 
  19. ;; You should have received a copy of the GNU General Public License
  20. ;; along with this program; if not, write to the Free Software
  21. ;; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  22.  
  23. ;;; Code:
  24.  
  25. (require 'tex)
  26.  
  27. ;;; Customization:
  28.  
  29. (defcustom TeX-process-asynchronous (not (eq system-type 'ms-dos))
  30.   "*Use asynchronous processes."
  31.   :group 'TeX-commands
  32.   :type 'boolean)
  33.  
  34. (defcustom TeX-shell
  35.   (if (memq system-type '(ms-dos emx windows-nt))
  36.       shell-file-name
  37.     "/bin/sh")
  38.   "Name of shell used to parse TeX commands."
  39.   :group 'TeX-commands
  40.   :type 'file)
  41.  
  42. (defcustom TeX-shell-command-option
  43.   (cond ((memq system-type '(ms-dos emx windows-nt) )
  44.      (cond ((boundp 'shell-command-option)
  45.         shell-command-option)
  46.            ((boundp 'shell-command-switch)
  47.         shell-command-switch)
  48.            (t 
  49.         "/c")))
  50.     (t                ;Unix & EMX (Emacs 19 port to OS/2)
  51.      "-c"))
  52.   "Shell argument indicating that next argument is the command."
  53.   :group 'TeX-commands
  54.   :type 'string)
  55.  
  56. ;;; Interactive Commands
  57. ;;
  58. ;; The general idea is, that there is one process and process buffer
  59. ;; associated with each master file, and one process and process buffer
  60. ;; for running TeX on a region.   Thus, if you have N master files, you
  61. ;; can run N + 1 processes simultaneously.  
  62. ;;
  63. ;; Some user commands operates on ``the'' process.  The following
  64. ;; algorithm determine what ``the'' process is.
  65. ;;
  66. ;; IF   last process started was on a region 
  67. ;; THEN ``the'' process is the region process
  68. ;; ELSE ``the'' process is the master file (of the current buffer) process
  69.  
  70. (defun TeX-save-document (name)
  71.   "Save all files belonging to the current document.
  72. Return non-nil if document need to be re-TeX'ed."
  73.   (interactive (list (TeX-master-file)))
  74.   (if (string-equal name "")
  75.       (setq name (TeX-master-file)))
  76.   
  77.   (TeX-check-files (concat name ".dvi")
  78.            (cons name (TeX-style-list))
  79.            TeX-file-extensions))
  80.  
  81. (defun TeX-command-master ()
  82.   "Run command on the current document."
  83.   (interactive)
  84.   (TeX-command (TeX-command-query (TeX-master-file)) 'TeX-master-file))
  85.  
  86. (defvar TeX-command-region-begin nil)
  87. (defvar TeX-command-region-end nil)
  88. ;; Used for marking the last region.
  89.  
  90. (make-variable-buffer-local 'TeX-command-region-begin)
  91. (make-variable-buffer-local 'TeX-command-region-end)
  92.  
  93. (defun TeX-command-region (&optional old)
  94.   "Run TeX on the current region.
  95.  
  96. Query the user for a command to run on the temporary file specified by
  97. the variable TeX-region.  If the chosen command is so marked in
  98. TeX-command-list, and no argument (or nil) is given to the command,
  99. the region file file be recreated with the current region.  If mark is
  100. not active, the new text in the previous used region will be used.
  101.  
  102. If the master file for the document has a header, it is written to the
  103. temporary file before the region itself.  The document's header is all
  104. text before TeX-header-end.
  105.  
  106. If the master file for the document has a trailer, it is written to
  107. the temporary file before the region itself.  The document's trailer is
  108. all text after TeX-trailer-start."
  109.   (interactive "P")
  110.   (if (and (TeX-mark-active) (not old))
  111.       (let ((begin (min (point) (mark)))
  112.          (end (max (point) (mark))))
  113.     (if TeX-command-region-begin
  114.         ()
  115.       (setq TeX-command-region-begin (make-marker)
  116.         TeX-command-region-end (make-marker)))
  117.     (set-marker TeX-command-region-begin begin)
  118.     (set-marker TeX-command-region-end end)))
  119.   (if (null TeX-command-region-begin)
  120.       (error "Mark not set"))
  121.   (let ((begin (marker-position TeX-command-region-begin))
  122.     (end (marker-position TeX-command-region-end)))
  123.     (TeX-region-create (TeX-region-file TeX-default-extension)
  124.                (buffer-substring begin end)
  125.                (file-name-nondirectory (buffer-file-name))
  126.                (count-lines (save-restriction (widen) (point-min))
  127.                     begin)))
  128.   (TeX-command (TeX-command-query (TeX-region-file)) 'TeX-region-file))
  129.  
  130. (defun TeX-command-buffer ()
  131.   "Run TeX on the current buffer.
  132.  
  133. Query the user for a command to run on the temporary file specified by
  134. the variable TeX-region.  The region file file be recreated from the
  135. visible part of the buffer."
  136.   (interactive)
  137.   (let ((TeX-command-region-begin (point-min-marker))
  138.     (TeX-command-region-end (point-max-marker)))
  139.     (TeX-command-region t)))
  140.  
  141. (defun TeX-recenter-output-buffer (line)
  142.   "Redisplay buffer of TeX job output so that most recent output can be seen.
  143. The last line of the buffer is displayed on line LINE of the window, or
  144. at bottom if LINE is nil." 
  145.   (interactive "P")
  146.   (let ((buffer (TeX-active-buffer)))
  147.     (if buffer
  148.     (let ((old-buffer (current-buffer)))
  149.       (pop-to-buffer buffer t)
  150.       (bury-buffer buffer)
  151.       (goto-char (point-max))
  152.       (recenter (if line
  153.             (prefix-numeric-value line)
  154.               (/ (window-height) 2)))
  155.       (pop-to-buffer old-buffer))
  156.       (message "No process for this document."))))
  157.  
  158. (defun TeX-kill-job ()
  159.   "Kill the currently running TeX job."
  160.   (interactive)
  161.   (let ((process (TeX-active-process)))
  162.     (if process
  163.     (kill-process process)
  164.       ;; Should test for TeX background process here.
  165.       (error "No TeX process to kill"))))
  166.  
  167. (defun TeX-home-buffer ()
  168.   "Go to the buffer where you last issued a TeX command.  
  169. If there is no such buffer, or you already are in that buffer, find
  170. the master file."
  171.   (interactive)
  172.   (if (or (null TeX-command-buffer)
  173.       (eq TeX-command-buffer (current-buffer)))
  174.       (find-file (TeX-master-file TeX-default-extension))
  175.     (switch-to-buffer TeX-command-buffer)))
  176.  
  177. (defun TeX-next-error (reparse)
  178.   "Find the next error in the TeX output buffer.
  179. Prefix by C-u to start from the beginning of the errors."
  180.   (interactive "P")
  181.   (if (null (TeX-active-buffer))
  182.       (error "No TeX output buffer")
  183.     (funcall (TeX-process-get-variable (TeX-active-master) 'TeX-parse-function)
  184.          reparse)))
  185.  
  186. (defun TeX-toggle-debug-boxes ()
  187.   "Toggle if the debugger should display \"bad boxes\" too."
  188.   (interactive)
  189.   (cond (TeX-debug-bad-boxes
  190.      (setq TeX-debug-bad-boxes nil))
  191.     (t
  192.      (setq TeX-debug-bad-boxes t)))
  193.   (message (concat "TeX-debug-bad-boxes: " (cond (TeX-debug-bad-boxes "on")
  194.                          (t "off")))))
  195.  
  196. ;;; Command Query
  197.  
  198. (defun TeX-command (name file)
  199.   "Run command NAME on the file you get by calling FILE.
  200.  
  201. FILE is a function return a file name.  It has one optional argument,
  202. the extension to use on the file.
  203.  
  204. Use the information in TeX-command-list to determine how to run the
  205. command."
  206.   (setq TeX-current-process-region-p (eq file 'TeX-region-file))
  207.   (let ((command (TeX-command-expand (nth 1 (assoc name TeX-command-list))
  208.                      file))
  209.     (hook (nth 2 (assoc name TeX-command-list)))
  210.     (confirm (nth 3 (assoc name TeX-command-list))))
  211.  
  212.     ;; Verify the expanded command
  213.     (if confirm
  214.     (setq command
  215.           (read-from-minibuffer (concat name " command: ") command)))
  216.     
  217.     ;; Now start the process
  218.     (TeX-process-set-variable name 'TeX-command-next TeX-command-Show)
  219.     (apply hook name command (apply file nil) nil)))
  220.  
  221. (defun TeX-command-expand (command file &optional list)
  222.   "Expand COMMAND for FILE as described in LIST.
  223. LIST default to TeX-expand-list."
  224.   (if (null list)
  225.       (setq list TeX-expand-list))
  226.   (while list
  227.     (let ((case-fold-search nil) ; Do not ignore case.
  228.       (string (car (car list)))    ;First element
  229.       (expansion (car (cdr (car list)))) ;Second element
  230.       (arguments (cdr (cdr (car list))))) ;Remaining elements
  231.       (while (string-match string command)
  232.     (let ((prefix (substring command 0 (match-beginning 0)))
  233.           (postfix (substring command (match-end 0))))
  234.       (setq command (concat prefix
  235.                 (cond ((TeX-function-p expansion)
  236.                        (apply expansion arguments))
  237.                       ((boundp expansion)
  238.                        (apply (eval expansion) arguments))
  239.                       (t
  240.                        (error "Nonexpansion %s" expansion)))
  241.                 postfix)))))
  242.     (setq list (cdr list)))
  243.   command)
  244.  
  245. (defun TeX-check-files (derived originals extensions)
  246.   "Check that DERIVED is newer than any of the ORIGINALS.
  247. Try each original with each member of EXTENSIONS, in all directories
  248. in TeX-check-path."
  249.   (let ((found nil)
  250.     (regexp (concat "\\`\\("
  251.             (mapconcat (function (lambda (dir)
  252.                       (regexp-quote (expand-file-name dir))))
  253.                    TeX-check-path "\\|")
  254.             "\\).*\\("
  255.             (mapconcat 'regexp-quote originals "\\|")
  256.             "\\)\\.\\("
  257.             (mapconcat 'regexp-quote extensions "\\|")
  258.             "\\)\\'"))
  259.     (buffers (buffer-list)))
  260.     (while buffers
  261.       (let* ((buffer (car buffers))
  262.          (name (buffer-file-name buffer)))
  263.     (setq buffers (cdr buffers))
  264.     (if (and name (string-match regexp name))
  265.         (progn
  266.           (and (buffer-modified-p buffer)
  267.            (or (not TeX-save-query)
  268.                (y-or-n-p (concat "Save file "
  269.                      (buffer-file-name buffer)
  270.                      "? ")))
  271.            (save-excursion (set-buffer buffer) (save-buffer)))
  272.           (if (file-newer-than-file-p name derived)
  273.           (setq found t))))))
  274.     found))
  275.  
  276. (defcustom TeX-save-query t
  277.   "*If non-nil, ask user for permission to save files before starting TeX."
  278.   :group 'TeX-commands
  279.   :type 'boolean)
  280.  
  281. (defun TeX-command-query (name)
  282.   "Query the user for a what TeX command to use."
  283.   (let* ((default (cond ((if (string-equal name TeX-region)
  284.                  (TeX-check-files (concat name ".dvi")
  285.                           (list name)
  286.                           TeX-file-extensions)
  287.                (TeX-save-document (TeX-master-file)))
  288.              TeX-command-default)
  289.             ((and (eq major-mode 'latex-mode)
  290.                   (TeX-check-files (concat name ".bbl")
  291.                            (mapcar 'car
  292.                                (LaTeX-bibliography-list))
  293.                            BibTeX-file-extensions))
  294.              ;; We should check for bst files here as well.
  295.              TeX-command-BibTeX)
  296.             ((TeX-process-get-variable name
  297.                            'TeX-command-next
  298.                            TeX-command-Show))
  299.             (TeX-command-Show)))
  300.      (completion-ignore-case t)
  301.      (answer (or TeX-command-force
  302.              (completing-read
  303.               (concat "Command: (default " default ") ")
  304.               TeX-command-list nil t))))
  305.     ;; If the answer "latex" it will not be expanded to "LaTeX"
  306.     (setq answer (car-safe (TeX-assoc answer TeX-command-list)))
  307.     (if (and answer
  308.          (not (string-equal answer "")))
  309.     answer
  310.       default)))
  311.  
  312. (defvar TeX-command-next nil
  313.   "The default command next time TeX-command is invoked.")
  314.  
  315.  (make-variable-buffer-local 'TeX-command-next)
  316.  
  317. (defun TeX-printer-query (&optional command element)
  318.   "Query the user for a printer name.
  319. COMMAND is the default command to use if the entry for the printer in
  320. TeX-printer-list does not itself have it specified in the ELEMENT'th
  321. entry." 
  322.   (or command (setq command TeX-print-command))
  323.   (or element (setq element 1))
  324.   (let ((printer (if TeX-printer-list
  325.              (let ((completion-ignore-case t))
  326.                (completing-read (concat "Printer: (default "
  327.                         TeX-printer-default ") ")
  328.                     TeX-printer-list))
  329.            "")))
  330.     
  331.     (setq printer (or (car-safe (TeX-assoc printer TeX-printer-list))
  332.               printer))
  333.     (if (or (null printer) (string-equal "" printer))
  334.     (setq printer TeX-printer-default)
  335.       (setq TeX-printer-default printer))
  336.  
  337.     (let ((expansion (let ((entry (assoc printer TeX-printer-list)))
  338.                (if (and entry (nth element entry))
  339.                (nth element entry)
  340.              command))))
  341.       (if (string-match "%p" printer)
  342.       (error "Don't use %s in printer names" "%p"))
  343.       (while (string-match "%p" expansion)
  344.     (setq expansion (concat (substring expansion 0 (match-beginning 0))
  345.                 printer
  346.                 (substring expansion (match-end 0)))))
  347.       expansion)))
  348.  
  349. (defun TeX-style-check (styles)
  350.   "Check STYLES compared to the current style options."
  351.  
  352.   (let ((files (TeX-style-list)))
  353.     (while (and styles
  354.         (not (TeX-member (car (car styles)) files 'string-match)))
  355.       (setq styles (cdr styles))))
  356.   (if styles
  357.       (nth 1 (car styles))
  358.     ""))
  359.  
  360. ;;; Command Hooks
  361.  
  362. (defvar TeX-after-start-process-function nil
  363.   "Hooks to run after starting an asynchronous process.
  364. Used by Japanese TeX to set the coding system.")
  365.  
  366. (defcustom TeX-show-compilation nil
  367.   "*If non-nil, show output of TeX compilation in other window."
  368.   :group 'TeX-commands
  369.   :type 'boolean)
  370.  
  371. (defun TeX-run-command (name command file)
  372.   "Create a process for NAME using COMMAND to process FILE.
  373. Return the new process."
  374.   (let ((default TeX-command-default)
  375.     (buffer (TeX-process-buffer-name file))
  376.     (dir (TeX-master-directory)))
  377.     (TeX-process-check file)        ; Check that no process is running
  378.     (setq TeX-command-buffer (current-buffer))
  379.     (get-buffer-create buffer)
  380.     (set-buffer buffer)
  381.     (erase-buffer)
  382.     (if dir (cd dir))
  383.     (insert "Running `" name "' on `" file "' with ``" command "''\n")
  384.     (setq mode-name name)
  385.     (if TeX-show-compilation
  386.     (display-buffer buffer)
  387.       (message "Type `C-c C-l' to display results of compilation."))
  388.     (setq TeX-parse-function 'TeX-parse-command)
  389.     (setq TeX-command-default default)
  390.     (setq TeX-sentinel-function
  391.       (function (lambda (process name)
  392.               (message (concat name ": done.")))))
  393.     (if TeX-process-asynchronous
  394.     (let ((process (start-process name buffer TeX-shell
  395.                       TeX-shell-command-option command)))
  396.       (if TeX-after-start-process-function
  397.           (funcall TeX-after-start-process-function process))
  398.       (TeX-command-mode-line process)
  399.       (set-process-filter process 'TeX-command-filter)
  400.       (set-process-sentinel process 'TeX-command-sentinel)
  401.       (set-marker (process-mark process) (point-max))
  402.       (setq compilation-in-progress (cons process compilation-in-progress))
  403.       process)
  404.       (setq mode-line-process ": run")
  405.       (set-buffer-modified-p (buffer-modified-p))
  406.       (sit-for 0)                ; redisplay
  407.       (call-process TeX-shell nil buffer nil
  408.             TeX-shell-command-option command))))
  409.  
  410. (defun TeX-run-format (name command file)
  411.   "Create a process for NAME using COMMAND to format FILE with TeX."
  412.   (let ((buffer (TeX-process-buffer-name file))
  413.     (process (TeX-run-command name command file)))
  414.     ;; Hook to TeX debuger.
  415.     (save-excursion
  416.       (set-buffer buffer)
  417.       (TeX-parse-reset)
  418.       (setq TeX-parse-function 'TeX-parse-TeX)
  419.       (setq TeX-sentinel-function 'TeX-TeX-sentinel)
  420.       (if TeX-process-asynchronous
  421.       (progn
  422.         ;; Updating the mode line.
  423.         (setq TeX-current-page "[0]")
  424.         (TeX-format-mode-line process)
  425.         (set-process-filter process 'TeX-format-filter)))
  426.       process)))
  427.  
  428. (defun TeX-run-TeX (name command file)
  429.   "Create a process for NAME using COMMAND to format FILE with TeX."
  430.   (let ((process (TeX-run-format name command file)))
  431.     (if TeX-process-asynchronous
  432.     process
  433.       (TeX-synchronous-sentinel name file process))))
  434.  
  435. (defun TeX-run-LaTeX (name command file)
  436.   "Create a process for NAME using COMMAND to format FILE with TeX."
  437.   (let ((process (TeX-run-format name command file)))
  438.     (setq TeX-sentinel-function 'TeX-LaTeX-sentinel)
  439.     (if TeX-process-asynchronous
  440.     process
  441.       (TeX-synchronous-sentinel name file process))))
  442.  
  443. (defun TeX-run-BibTeX (name command file)
  444.   "Create a process for NAME using COMMAND to format FILE with BibTeX."
  445.   (let ((process (TeX-run-command name command file)))
  446.     (setq TeX-sentinel-function 'TeX-BibTeX-sentinel)
  447.     (if TeX-process-asynchronous
  448.     process
  449.       (TeX-synchronous-sentinel name file process))))
  450.  
  451. (defun TeX-run-compile (name command file)
  452.   "Ignore first and third argument, start compile with second argument."
  453.   (compile command))
  454.  
  455. (defun TeX-run-shell (name command file)
  456.   "Ignore first and third argument, start shell-command with second argument."
  457.   (shell-command command)
  458.   (if (eq system-type 'ms-dos)
  459.       (redraw-display)))
  460.  
  461. (defun TeX-run-discard (name command file)
  462.   "Start process with second argument, discarding its output."
  463.   (process-kill-without-query (start-process (concat name " discard")
  464.                          nil TeX-shell
  465.                          TeX-shell-command-option
  466.                          command)))
  467.  
  468. (defun TeX-run-dviout (name command file)
  469.   "Call process wbith second argument, discarding its output. With support
  470. for the dviout previewer, especially when used with PC-9801 series."
  471.     (if (and (boundp 'dos-machine-type) (eq dos-machine-type 'pc98)) ;if PC-9801
  472.       (send-string-to-terminal "\e[2J")) ; clear screen
  473.     (call-process TeX-shell (if (eq system-type 'ms-dos) "con") nil nil
  474.                 TeX-shell-command-option command)
  475.     (if (eq system-type 'ms-dos)
  476.       (redraw-display)))
  477.  
  478. (defun TeX-run-background (name command file)
  479.   "Start process with second argument, show output when and if it arrives."
  480.   (let ((dir (TeX-master-directory)))
  481.     (set-buffer (get-buffer-create "*TeX background*"))
  482.     (if dir (cd dir))
  483.     (erase-buffer)
  484.     (let ((process (start-process (concat name " background")
  485.                   nil TeX-shell
  486.                   TeX-shell-command-option command)))
  487.       (if TeX-after-start-process-function
  488.       (funcall TeX-after-start-process-function process))
  489.       (set-process-filter process 'TeX-background-filter)
  490.       (process-kill-without-query process))))
  491.  
  492. (defun TeX-run-interactive (name command file)
  493.   "Run TeX interactively.
  494. Run command in a buffer (in comint-shell-mode) so that it accepts user
  495. interaction. If you return to the file buffer after the TeX run,
  496. Error parsing on C-x ` should work with a bit of luck."
  497.   (require 'comint)
  498.   (let ((default TeX-command-default)
  499.     (buffer (TeX-process-buffer-name file))
  500.     (process nil)
  501.     (dir (TeX-master-directory)))
  502.     (TeX-process-check file)        ; Check that no process is running
  503.     (setq TeX-command-buffer (current-buffer))
  504.     (with-output-to-temp-buffer buffer)
  505.     (set-buffer buffer)
  506.     (if dir (cd dir))
  507.     (insert "Running `" name "' on `" file "' with ``" command "''\n")
  508.     (comint-exec buffer name TeX-shell nil
  509.          (list TeX-shell-command-option command))
  510.     (comint-mode)
  511.     (setq mode-name name)
  512.     (setq TeX-command-default default)
  513.     (setq process (get-buffer-process buffer))
  514.     (if TeX-after-start-process-function
  515.     (funcall TeX-after-start-process-function process))
  516.     (TeX-command-mode-line process)
  517.     (set-process-sentinel process 'TeX-command-sentinel)
  518.     (set-marker (process-mark process) (point-max))
  519.     (setq compilation-in-progress (cons process compilation-in-progress))
  520.     (TeX-parse-reset)
  521.     (setq TeX-parse-function 'TeX-parse-TeX)
  522.     (setq TeX-sentinel-function 'TeX-LaTeX-sentinel)))
  523.  
  524. ;;; Command Sentinels
  525.  
  526. (defun TeX-synchronous-sentinel (name file result)
  527.   "Process TeX command output buffer after the process dies."
  528.   (let* ((buffer (TeX-process-buffer file)))
  529.     (save-excursion
  530.       (set-buffer buffer)
  531.       
  532.       ;; Append post-mortem information to the buffer
  533.       (goto-char (point-max))
  534.       (insert "\n" mode-name (if (and result (zerop result))
  535.                  " finished" " exited") " at "
  536.           (substring (current-time-string) 0 -5))
  537.       (setq mode-line-process ": exit")
  538.       
  539.       ;; Do command specific actions.
  540.       (setq TeX-command-next TeX-command-Show)
  541.       (goto-char (point-min))
  542.       (apply TeX-sentinel-function nil name nil)
  543.       
  544.       ;; Force mode line redisplay soon
  545.       (set-buffer-modified-p (buffer-modified-p)))))
  546.  
  547. (defun TeX-command-sentinel (process msg)
  548.   "Process TeX command output buffer after the process dies."
  549.   (let* ((buffer (process-buffer process))
  550.      (name (process-name process)))
  551.     (cond ((null (buffer-name buffer))    ; buffer killed
  552.        (set-process-buffer process nil)
  553.        (set-process-sentinel process nil))
  554.       ((memq (process-status process) '(signal exit))
  555.        (save-excursion
  556.          (set-buffer buffer)
  557.          
  558.          ;; Append post-mortem information to the buffer
  559.          (goto-char (point-max))
  560.          (insert "\n" mode-name " " msg)
  561.          (forward-char -1)
  562.          (insert " at "
  563.              (substring (current-time-string) 0 -5))
  564.          (forward-char 1)
  565.          
  566.          ;; Do command specific actions.
  567.          (TeX-command-mode-line process)
  568.          (setq TeX-command-next TeX-command-Show)
  569.          (goto-char (point-min))
  570.          (apply TeX-sentinel-function process name nil)
  571.          
  572.          
  573.          ;; If buffer and mode line will show that the process
  574.          ;; is dead, we can delete it now.  Otherwise it
  575.          ;; will stay around until M-x list-processes.
  576.          (delete-process process)
  577.          
  578.          ;; Force mode line redisplay soon
  579.          (set-buffer-modified-p (buffer-modified-p))))))
  580.   (setq compilation-in-progress (delq process compilation-in-progress)))
  581.  
  582.  
  583. (defvar TeX-sentinel-function (function (lambda (process name)))
  584.   "Hook to cleanup TeX command buffer after temination of PROCESS.
  585. NAME is the name of the process.")
  586.  
  587.   (make-variable-buffer-local 'TeX-sentinel-function)
  588.  
  589. (defun TeX-TeX-sentinel (process name)
  590.   "Cleanup TeX output buffer after running TeX."
  591.   (if (TeX-TeX-sentinel-check process name)
  592.       ()
  593.     (message (concat name ": formatted " (TeX-current-pages)))
  594.     (setq TeX-command-next TeX-command-Show)))
  595.  
  596. (defun TeX-current-pages ()
  597.   ;; String indictating the number of pages formatted.
  598.   (cond ((null TeX-current-page)
  599.      "some pages.")
  600.     ((string-match "[^0-9]1[^0-9]" TeX-current-page)
  601.      (concat TeX-current-page " page."))
  602.     (t
  603.      (concat TeX-current-page " pages."))))
  604.  
  605. (defun TeX-TeX-sentinel-check (process name)
  606.   "Cleanup TeX output buffer after running TeX.
  607. Return nil ifs no errors were found."
  608.   (save-excursion
  609.     (goto-char (point-max))
  610.     (if (re-search-backward "^Output written on.* (\\([0-9]+\\) page" nil t)
  611.     (setq TeX-current-page (concat "{" (TeX-match-buffer 1) "}"))))
  612.   (if process (TeX-format-mode-line process))
  613.   (if (re-search-forward "^! " nil t)
  614.       (progn
  615.     (message (concat name " errors in `" (buffer-name)
  616.              "'. Use C-c ` to display."))
  617.     (setq TeX-command-next TeX-command-default)
  618.     t)
  619.     (setq TeX-command-next TeX-command-Show)
  620.     nil))
  621.  
  622. (defun TeX-LaTeX-sentinel (process name)
  623.   "Cleanup TeX output buffer after running LaTeX."
  624.   (cond ((TeX-TeX-sentinel-check process name))
  625.     ((and (save-excursion
  626.         (re-search-forward "^LaTeX Warning: Citation" nil t))
  627.           (let ((current (current-buffer)))
  628.         (set-buffer TeX-command-buffer)
  629.         (prog1 (and (LaTeX-bibliography-list)
  630.                 (TeX-check-files (TeX-master-file "bbl")
  631.                         (TeX-style-list)
  632.                         (append TeX-file-extensions
  633.                             BibTeX-file-extensions)))
  634.           (set-buffer current))))
  635.      (message (concat "You should run BibTeX to get citations right, "
  636.                           (TeX-current-pages)))
  637.      (setq TeX-command-next TeX-command-BibTeX))
  638.     ((re-search-forward "^LaTeX Warning: Label(s)" nil t)
  639.      (message (concat "You should run LaTeX again "
  640.               "to get references right, "
  641.                           (TeX-current-pages)))
  642.      (setq TeX-command-next TeX-command-default))
  643.     ((re-search-forward "^LaTeX Warning: Reference" nil t)
  644.      (message (concat name ": there were unresolved references, "
  645.                           (TeX-current-pages)))
  646.      (setq TeX-command-next TeX-command-Show))
  647.     ((re-search-forward "^LaTeX Warning: Citation" nil t)
  648.      (message (concat name ": there were unresolved citations, "
  649.                           (TeX-current-pages)))
  650.      (setq TeX-command-next TeX-command-Show))
  651.     ((re-search-forward
  652.       "^\\(\\*\\* \\)?J?I?p?\\(La\\|Sli\\)TeX\\(2e\\)? \\(Version\\|ver\\.\\|<[0-9/]*>\\)" nil t)
  653.      (message (concat name ": successfully formatted "
  654.               (TeX-current-pages)))
  655.      (setq TeX-command-next TeX-command-Show))
  656.     (t
  657.      (message (concat name ": problems after "
  658.               (TeX-current-pages)))
  659.      (setq TeX-command-next TeX-command-default))))
  660.  
  661. (defun TeX-BibTeX-sentinel (process name)
  662.   "Cleanup TeX output buffer after running BibTeX."
  663.   (message "You should perhaps run LaTeX again to get citations right.")
  664.   (setq TeX-command-next TeX-command-default))
  665.  
  666. ;;; Process Control
  667.  
  668.  
  669. ;; This variable is chared with `compile.el'.
  670. (defvar compilation-in-progress nil
  671.   "List of compilation processes now running.")
  672.  
  673. (or (assq 'compilation-in-progress minor-mode-alist)
  674.     (setq minor-mode-alist (cons '(compilation-in-progress " Compiling")
  675.                  minor-mode-alist)))
  676.  
  677. (defun TeX-process-get-variable (name symbol &optional default)
  678.   "Return the value in the process buffer for NAME of SYMBOL.
  679.  
  680. Return DEFAULT if the process buffer does not exist or SYMBOL is not
  681. defined."
  682.   (let ((buffer (TeX-process-buffer name)))
  683.     (if buffer
  684.     (save-excursion
  685.       (set-buffer buffer)
  686.       (if (boundp symbol)
  687.           (eval symbol)
  688.         default))
  689.       default)))
  690.  
  691. (defun TeX-process-set-variable (name symbol value)
  692.   "Set the variable SYMBOL in the process buffer to VALUE.
  693. Return nil iff no process buffer exist."
  694.   (let ((buffer (TeX-process-buffer name)))
  695.     (if buffer
  696.     (save-excursion
  697.       (set-buffer buffer)
  698.       (set symbol value)
  699.       t)
  700.       nil)))
  701.  
  702. (defun TeX-process-check (name)
  703.   "Check if a process for the TeX document NAME already exist.
  704. If so, give the user the choice of aborting the process or the current
  705. command."
  706.   (let ((process (TeX-process name)))
  707.     (cond ((null process))
  708.       ((not (eq (process-status process) 'run)))
  709.       ((yes-or-no-p (concat "Process `"
  710.                 (process-name process)
  711.                 "' for document `"
  712.                 name
  713.                 "' running, kill it? "))
  714.        (delete-process process))
  715.       (t
  716.        (error "Cannot have two processes for the same document")))))
  717.  
  718. (defun TeX-process-buffer-name (name)
  719.   "Return name of AUC TeX buffer associated with the document NAME."
  720.   (concat "*" (abbreviate-file-name (expand-file-name name)) " output*"))
  721.  
  722. (defun TeX-process-buffer (name)
  723.   "Return the AUC TeX buffer associated with the document NAME."
  724.   (get-buffer (TeX-process-buffer-name name)))
  725.  
  726. (defun TeX-process (name)
  727.   "Return AUC TeX process associated with the document NAME."
  728.   (and TeX-process-asynchronous
  729.        (get-buffer-process (TeX-process-buffer name))))
  730.  
  731. ;;; Process Filters
  732.  
  733. (defun TeX-command-mode-line (process)
  734.   "Format the mode line for a buffer containing output from PROCESS."
  735.     (setq mode-line-process (concat ": "
  736.                     (symbol-name (process-status process))))
  737.     (set-buffer-modified-p (buffer-modified-p)))
  738.  
  739. (defun TeX-command-filter (process string)
  740.   "Filter to process normal output."
  741.   (save-excursion
  742.     (set-buffer (process-buffer process))
  743.     (save-excursion
  744.       (goto-char (process-mark process))
  745.       (insert-before-markers string)
  746.       (set-marker (process-mark process) (point)))))
  747.  
  748. (defvar TeX-current-page nil
  749.   "The page number currently being formatted, enclosed in brackets.")
  750.  
  751.  (make-variable-buffer-local 'TeX-current-page)
  752.  
  753. (defun TeX-format-mode-line (process)
  754.   "Format the mode line for a buffer containing TeX output from PROCESS."
  755.     (setq mode-line-process (concat " " TeX-current-page ": "
  756.                     (symbol-name (process-status process))))
  757.     (set-buffer-modified-p (buffer-modified-p)))
  758.  
  759. (defun TeX-format-filter (process string)
  760.   "Filter to process TeX output."
  761.   (save-excursion
  762.     (set-buffer (process-buffer process))
  763.     (save-excursion
  764.       (goto-char (process-mark process))
  765.       (insert-before-markers string)
  766.       (set-marker (process-mark process) (point))) 
  767.     (save-excursion
  768.       (save-match-data
  769.     (if (re-search-backward "\\[[0-9]+\\(\\.[0-9\\.]+\\)?\\]" nil t)
  770.         (setq TeX-current-page (TeX-match-buffer 0)))))
  771.     (TeX-format-mode-line process)))
  772.  
  773. (defvar TeX-parse-function nil
  774.   "Function to call to parse content of TeX output buffer.")
  775.  (make-variable-buffer-local 'TeX-parse-function)
  776.  
  777. (defun TeX-background-filter (process string)
  778.   "Filter to process background output."
  779.   (let ((old-window (selected-window))
  780.     (pop-up-windows t))
  781.     (pop-to-buffer "*TeX background*")
  782.     (goto-char (point-max))
  783.     (insert string)
  784.     (select-window old-window)))
  785.  
  786.  
  787. ;;; Active Process
  788.  
  789. (defvar TeX-current-process-region-p nil
  790.   "This variable is set to t iff the last TeX command is on a region.")
  791.  
  792. (defun TeX-active-process ()
  793.   "Return the active process for the current buffer."
  794.   (if TeX-current-process-region-p
  795.       (TeX-process (TeX-region-file))
  796.     (TeX-process (TeX-master-file))))
  797.  
  798. (defun TeX-active-buffer ()
  799.   "Return the buffer of the active process for this buffer."
  800.   (if TeX-current-process-region-p
  801.       (TeX-process-buffer (TeX-region-file))
  802.     (TeX-process-buffer (TeX-master-file))))
  803.  
  804. (defun TeX-active-master (&optional extension)
  805.   "The master file currently being compiled."
  806.   (if TeX-current-process-region-p
  807.       (TeX-region-file extension)
  808.     (TeX-master-file extension)))
  809.  
  810. (defvar TeX-command-buffer nil
  811.   "The buffer from where the last TeX command was issued.")
  812.  
  813. ;;; Region File
  814.  
  815. (defun TeX-region-create (file region original offset)
  816.   "Create a new file named FILE with the string REGION
  817. The region is taken from ORIGINAL starting at line OFFSET.
  818.  
  819. The current buffer and master file is searched, in order to ensure
  820. that the TeX header and trailer information is also included.
  821.  
  822. The OFFSET is used to provide the debugger with information about the
  823. original file."
  824.   (let* (;; We shift buffer a lot, so we must keep track of the buffer
  825.      ;; local variables.  
  826.      (header-end TeX-header-end)
  827.      (trailer-start TeX-trailer-start)
  828.      
  829.      ;; We seach for header and trailer in the master file.
  830.      (master-name (TeX-master-file TeX-default-extension))
  831.      (master-buffer (find-file-noselect master-name))
  832.  
  833.      ;; Attempt to disable font lock.
  834.      (font-lock-defaults-alist nil)
  835.      (font-lock-defaults nil)
  836.      (font-lock-maximum-size 0)
  837.      (font-lock-mode-hook nil)    
  838.      (font-lock-auto-fontify nil)
  839.      (font-lock-mode-enable-list nil)
  840.      ;; And insert them into the FILE buffer.
  841.      (file-buffer (find-file-noselect file))
  842.      ;; But remember original content.
  843.      original-content
  844.  
  845.      ;; We search for the header from the master file, if it is
  846.      ;; not present in the region.
  847.      (header (if (string-match header-end region)
  848.              ""
  849.            (save-excursion
  850.              (save-restriction
  851.                (set-buffer master-buffer)
  852.                (save-excursion
  853.              (save-restriction
  854.                (widen)
  855.                (goto-char (point-min))
  856.                ;; NOTE: We use the local value of
  857.                ;; TeX-header-end from the master file.
  858.                (if (not (re-search-forward TeX-header-end nil t))
  859.                    ""
  860.                  (re-search-forward "[\r\n]" nil t)
  861.                  (buffer-substring (point-min) (point)))))))))
  862.      
  863.      ;; We search for the trailer from the master file, if it is
  864.      ;; not present in the region.
  865.      (trailer-offset 0)
  866.      (trailer (if (string-match trailer-start region)
  867.               ""
  868.             (save-excursion
  869.               (save-restriction
  870.             (set-buffer master-buffer)
  871.             (save-excursion
  872.               (save-restriction
  873.                 (widen)
  874.                 (goto-char (point-max))
  875.                 ;; NOTE: We use the local value of
  876.                 ;; TeX-trailer-start from the master file.
  877.                 (if (not (re-search-backward TeX-trailer-start nil t))
  878.                 ""
  879.                   ;;(beginning-of-line 1)
  880.                   (re-search-backward "[\r\n]" nil t)
  881.                   (setq trailer-offset
  882.                     (count-lines (point-min) (point)))
  883.                   (buffer-substring (point) (point-max))))))))))
  884.     (save-excursion
  885.       (set-buffer file-buffer)
  886.       (setq original-content (buffer-string))
  887.       (erase-buffer)
  888.       (insert "\\message{ !name(" master-name ")}"
  889.           header
  890.           "\n\\message{ !name(" original ") !offset(")
  891.       (insert (int-to-string (- offset
  892.                 (count-lines (point-min) (point))))
  893.           ") }\n"
  894.           region
  895.           "\n\\message{ !name("  master-name ") !offset(")
  896.       (insert (int-to-string (- trailer-offset
  897.                 (count-lines (point-min) (point))))
  898.           ") }\n"
  899.           trailer)
  900.       (if (string-equal (buffer-string) original-content)
  901.       (set-buffer-modified-p nil)
  902.     (save-buffer 0)))))
  903.  
  904. (defun TeX-region-file (&optional extension nondirectory)
  905.   "Return TeX-region file name with EXTENSION.
  906. If optional second argument NONDIRECTORY is nil, do not include 
  907. the directory."
  908.   (concat (if nondirectory "" (TeX-master-directory))
  909.       (cond ((eq extension t)
  910.          (concat TeX-region "." TeX-default-extension))
  911.         (extension
  912.          (concat TeX-region "." extension))
  913.         (t
  914.          TeX-region))))
  915.  
  916. (defcustom TeX-region "_region_"
  917.   "*Base name for temporary file for use with TeX-region."
  918.   :group 'TeX-commands
  919.   :type 'string)
  920.  
  921. ;;; Parsing
  922.  
  923. ;;; - Global Parser Variables
  924.  
  925. (defvar TeX-error-point nil
  926.   "How far we have parsed until now.")
  927.  
  928.  (make-variable-buffer-local 'TeX-error-point)
  929.  
  930. (defvar TeX-error-file nil
  931.   "Stack of files in which errors have occured")
  932.  
  933.  (make-variable-buffer-local 'TeX-error-file)
  934.  
  935. (defvar TeX-error-offset nil
  936.   "Add this to any line numbers from TeX.  Stack like TeX-error-file.")
  937.  
  938.  (make-variable-buffer-local 'TeX-error-offset)
  939.  
  940. (defun TeX-parse-reset ()
  941.   "Reset all variables used for parsing TeX output."
  942.   (setq TeX-error-point (point-min))
  943.   (setq TeX-error-offset nil)
  944.   (setq TeX-error-file nil))
  945.  
  946. ;;; - Parsers Hooks
  947.  
  948. (defun TeX-parse-command (reparse)
  949.   "We can't parse anything but TeX."
  950.   (error "I cannot parse %s output, sorry"
  951.      (if (TeX-active-process)
  952.          (process-name (TeX-active-process))
  953.        "this")))
  954.  
  955. (defun TeX-parse-TeX (reparse)
  956.   "Find the next error produced by running TeX.
  957. Prefix by C-u to start from the beginning of the errors.
  958.  
  959. If the file occurs in an included file, the file is loaded (if not
  960. already in an Emacs buffer) and the cursor is placed at the error."
  961.  
  962.   (let ((old-buffer (current-buffer)))
  963.     (pop-to-buffer (TeX-active-buffer))
  964.     (if reparse
  965.     (TeX-parse-reset))
  966.     (goto-char TeX-error-point)
  967.     (TeX-parse-error old-buffer)))
  968.  
  969. ;;; - Parsing (La)TeX
  970.  
  971. (defvar TeX-translate-location-hook nil
  972.   "List of functions to be called before showing an error or warning.
  973.  
  974. You might want to examine and modify the free variables `file',
  975. `offset', `line', `string', `error', and `context' from this hook.")
  976.  
  977. (defun TeX-parse-error (old)
  978.   "Goto next error.  Pop to OLD buffer if no more errors are found."
  979.     (while
  980.     (progn
  981.       (re-search-forward (concat "\\("
  982.                      "^! \\|"
  983.                      "(\\|"
  984.                      ")\\|"
  985.                      "\\'\\|"
  986.                      "!offset([---0-9]*)\\|"
  987.                      "!name([^)]*)\\|"
  988.                      "^.*erfull \\\\.*[0-9]*--[0-9]*\\|"
  989.                      "^LaTeX Warning: .*[0-9]+\\.$"
  990.                      "\\)"))
  991.       (let ((string (TeX-match-buffer 1)))
  992.  
  993.         (cond (;; TeX error
  994.            (string= string "! ")
  995.            (TeX-error)
  996.            nil)
  997.  
  998.           ;; LaTeX warning
  999.           ((string-match (concat "\\("
  1000.                      "^.*erfull \\\\.*[0-9]*--[0-9]*\\|"
  1001.                      "^LaTeX Warning: .*[0-9]+\\.$"
  1002.                      "\\)")
  1003.  
  1004.                  string)
  1005.            (TeX-warning string))
  1006.  
  1007.           ;; New file -- Push on stack
  1008.           ((string= string "(")
  1009.            (re-search-forward "\\([^()\n \t]*\\)")
  1010.            (setq TeX-error-file
  1011.              (cons (TeX-match-buffer 1) TeX-error-file))
  1012.            (setq TeX-error-offset (cons 0 TeX-error-offset))
  1013.            t)
  1014.  
  1015.           ;; End of file -- Pop from stack
  1016.           ((string= string ")")
  1017.            (setq TeX-error-file (cdr TeX-error-file))
  1018.            (setq TeX-error-offset (cdr TeX-error-offset))
  1019.            t)
  1020.  
  1021.           ;; Hook to change line numbers
  1022.           ((string-match "!offset(\\([---0-9]*\\))" string)
  1023.            (rplaca TeX-error-offset
  1024.                (string-to-int (substring string
  1025.                              (match-beginning 1)
  1026.                              (match-end 1))))
  1027.            t)
  1028.  
  1029.           ;; Hook to change file name
  1030.           ((string-match "!name(\\([^)]*\\))" string)
  1031.            (rplaca TeX-error-file (substring string
  1032.                              (match-beginning 1)
  1033.                              (match-end 1)))
  1034.            t)
  1035.  
  1036.           ;; No more errors.
  1037.           (t
  1038.            (message "No more errors.")
  1039.            (beep)
  1040.            (pop-to-buffer old)
  1041.            nil))))))
  1042.  
  1043. (defun TeX-error ()
  1044.   "Display an error."
  1045.  
  1046.   (let* (;; We need the error message to show the user.
  1047.      (error (progn
  1048.           (re-search-forward "\\(.*\\)")
  1049.           (TeX-match-buffer 1)))
  1050.  
  1051.      ;; And the context for the help window.
  1052.      (context-start (point))
  1053.  
  1054.      ;; And the line number to position the cursor.
  1055.      (line (if (re-search-forward "l\\.\\([0-9]+\\)" nil t)
  1056.            (string-to-int (TeX-match-buffer 1))
  1057.          1))
  1058.      ;; And a string of the context to search for.
  1059.      (string (progn
  1060.            (beginning-of-line)
  1061.            (re-search-forward " \\(\\([^ \t]*$\\)\\|\\($\\)\\)")
  1062.            (TeX-match-buffer 1)))
  1063.  
  1064.      ;; And we have now found to the end of the context. 
  1065.      (context (buffer-substring context-start (progn 
  1066.                             (forward-line 1)
  1067.                             (end-of-line)
  1068.                             (point))))
  1069.      ;; We may use these in another buffer.
  1070.      (offset (car TeX-error-offset) )
  1071.      (file (car TeX-error-file)))
  1072.      
  1073.     ;; Remember where we was.
  1074.     (setq TeX-error-point (point))
  1075.  
  1076.     ;; Find the error.
  1077.     (if (null file)
  1078.     (error "Error occured after last TeX file closed"))
  1079.     (run-hooks 'TeX-translate-location-hook)
  1080.     (find-file-other-window file)
  1081.     (goto-line (+ offset line))
  1082.     (if (not (string= string " "))
  1083.     (search-forward string nil t))
  1084.  
  1085.     ;; Explain the error.
  1086.     (if TeX-display-help
  1087.     (TeX-help-error error context)
  1088.       (message (concat "! " error)))))
  1089.  
  1090. (defun TeX-warning (string)
  1091.   "Display a warning for STRING.
  1092. Return nil if we gave a report."
  1093.  
  1094.   (let* ((error (concat "** " string))
  1095.  
  1096.      ;; bad-box is nil if this is a "LaTeX Warning"
  1097.      (bad-box (string-match "^.*erfull \\\\.*[0-9]*--[0-9]*" string))
  1098.      ;; line-string: match 1 is beginning line, match 2 is end line
  1099.      (line-string (if bad-box " \\([0-9]*\\)--\\([0-9]*\\)"
  1100.             "on input line \\([0-9]*\\)\\."))
  1101.      ;; word-string: match 1 is the word
  1102.      (word-string (if bad-box "[][\\W() ---]\\(\\w+\\)[][\\W() ---]*$"
  1103.             "`\\(\\w+\\)'"))
  1104.  
  1105.      ;; Get error-line (warning)
  1106.      (line (progn
  1107.          (re-search-backward line-string)
  1108.          (string-to-int (TeX-match-buffer 1))))
  1109.      (line-end (if bad-box (string-to-int (TeX-match-buffer 2))
  1110.              line))
  1111.      
  1112.      ;; Find the context
  1113.      (context-start (progn (if bad-box (end-of-line)
  1114.                  (beginning-of-line))
  1115.                    (point)))
  1116.  
  1117.      (context (progn
  1118.             (forward-line 1)
  1119.             (end-of-line)
  1120.             (while (equal (current-column) 79)
  1121.               (forward-line 1)
  1122.               (end-of-line))
  1123.             (buffer-substring context-start (point))))
  1124.  
  1125.      ;; This is where we want to be.
  1126.      (error-point (point))
  1127.  
  1128.      ;; Now find the error word.
  1129.      (string (progn
  1130.            (re-search-backward word-string
  1131.                        context-start t)
  1132.            (TeX-match-buffer 1)))
  1133.  
  1134.      ;; We might use these in another file.
  1135.      (offset (car TeX-error-offset))
  1136.      (file (car TeX-error-file)))
  1137.  
  1138.     ;; This is where we start next time.
  1139.     (goto-char error-point)
  1140.     (setq TeX-error-point (point))
  1141.  
  1142.     ;; Go back to TeX-buffer
  1143.     (if TeX-debug-bad-boxes
  1144.     (progn
  1145.       (run-hooks 'TeX-translate-location-hook)
  1146.       (find-file-other-window file)
  1147.       ;; Find line and string
  1148.       (goto-line (+ offset line))
  1149.       (beginning-of-line 0)
  1150.       (let ((start (point)))
  1151.         (goto-line (+ offset line-end))
  1152.         (end-of-line)
  1153.         (search-backward string start t)
  1154.         (search-forward string nil t))
  1155.       ;; Display help
  1156.       (if TeX-display-help
  1157.           (TeX-help-error error (if bad-box context (concat "\n" context)))
  1158.         (message (concat "! " error)))
  1159.       nil)
  1160.       t)))
  1161.  
  1162. ;;; - Help
  1163.  
  1164. (defun TeX-help-error (error output)
  1165.   "Print ERROR in context OUTPUT in another window."
  1166.  
  1167.   (let ((old-buffer (current-buffer))
  1168.     (log-file (TeX-active-master "log"))
  1169.     (TeX-error-pointer 1))
  1170.  
  1171.     ;; Find help text entry.
  1172.     (while (not (string-match (car (nth TeX-error-pointer 
  1173.                     TeX-error-description-list))
  1174.                   error))
  1175.       (setq TeX-error-pointer (+ TeX-error-pointer 1)))
  1176.  
  1177.     (pop-to-buffer (get-buffer-create "*TeX Help*"))
  1178.     (erase-buffer)
  1179.     (insert "ERROR: " error
  1180.         "\n\n--- TeX said ---"
  1181.         output
  1182.         "\n--- HELP ---\n"
  1183.         (save-excursion
  1184.           (if (and (string= (cdr (nth TeX-error-pointer
  1185.                       TeX-error-description-list))
  1186.                 "No help available")
  1187.                (let* ((log-buffer (find-file-noselect log-file)))
  1188.              (set-buffer log-buffer)
  1189.              (auto-save-mode nil)
  1190.              (setq buffer-read-only t)
  1191.              (goto-line (point-min))
  1192.              (search-forward error nil t 1)))
  1193.           (progn
  1194.             (re-search-forward "^l.")
  1195.             (re-search-forward "^ [^\n]+$")
  1196.             (forward-char 1)
  1197.             (let ((start (point)))
  1198.               (re-search-forward "^$")
  1199.               (concat "From the .log file...\n\n"
  1200.                   (buffer-substring start (point)))))
  1201.         (cdr (nth TeX-error-pointer
  1202.               TeX-error-description-list)))))
  1203.     (goto-char (point-min))
  1204.     (pop-to-buffer old-buffer)))
  1205.  
  1206. ;;; Error Messages
  1207.  
  1208. (defcustom TeX-error-description-list
  1209.   '(("Bad \\\\line or \\\\vector argument.*" .
  1210. "The first argument of a \\line or \\vector command, which specifies the
  1211. slope, is illegal\.")
  1212.  
  1213.     ("Bad math environment delimiter.*" .
  1214. "TeX has found either a math-mode-starting command such as \\[ or \\(
  1215. when it is already in math mode, or else a math-mode-ending command
  1216. such as \\) or \\] while in LR or paragraph mode.  The problem is caused
  1217. by either unmatched math mode delimiters or unbalanced braces\.")
  1218.  
  1219.     ("Bad use of \\\\\\\\.*" .
  1220. "A \\\\ command appears between paragraphs, where it makes no sense. This
  1221. error message occurs when the \\\\ is used in a centering or flushing
  1222. environment or else in the scope of a centering or flushing
  1223. declaration.")
  1224.  
  1225.     ("\\\\begin{[^ ]*} ended by \\\\end{[^ ]*}." .
  1226. "LaTeX has found an \\end command that doesn't match the corresponding
  1227. \\begin command. You probably misspelled the environment name in the
  1228. \\end command, have an extra \\begin, or else forgot an \\end.")
  1229.  
  1230.     ("Can be used only in preamble." .
  1231. "LaTeX has encountered, after the \\begin{document}, one of the
  1232. following commands that should appear only in the preamble:
  1233. \\documentstyle, \\nofiles, \\includeonly, \\makeindex, or
  1234. \\makeglossary.  The error is also caused by an extra \\begin{document}
  1235. command.")
  1236.  
  1237.     ("Command name [^ ]* already used.*" .
  1238. "You are using \\newcommand, \\newenvironment, \\newlength, \\newsavebox,
  1239. or \\newtheorem to define a command or environment name that is
  1240. already defined, or \\newcounter to define a counter that already
  1241. exists. (Defining an environment named gnu automatically defines the
  1242. command \\gnu.) You'll have to choose a new name or, in the case of
  1243. \\newcommand or \\newenvironment, switch to the \\renew ...  command.")
  1244.  
  1245.     ("Counter too large." .
  1246. "Some object that is numbered with letters, probably an item in a
  1247. enumerated list, has received a number greater than 26. Either you're
  1248. making a very long list or you've been resetting counter values.")
  1249.  
  1250.     ("Environment [^ ]* undefined." .
  1251. "LaTeX has encountered a \\begin command for a nonexistent environment.
  1252. You probably misspelled the environment name. ")
  1253.  
  1254.     ("Float(s) lost." .
  1255. "You put a figure or table environment or a \\marginpar command inside a
  1256. parbox---either one made with a minipage environment or \\parbox
  1257. command, or one constructed by LaTeX in making a footnote, figure,
  1258. etc. This is an outputting error, and the offending environment or
  1259. command may be quite a way back from the point where LaTeX discovered
  1260. the problem. One or more figures, tables, and/or marginal notes have
  1261. been lost, but not necessarily the one that caused the error.")
  1262.  
  1263.     ("Illegal character in array arg." .
  1264. "There is an illegal character in the argument of an array or tabular
  1265. environment, or in the second argument of a \\multicolumn command.")
  1266.  
  1267.     ("Missing \\\\begin{document}." .
  1268. "LaTeX produced printed output before encountering a \\begin{document}
  1269. command. Either you forgot the \\begin{document} command or there is
  1270. something wrong in the preamble. The problem may be a stray character
  1271. or an error in a declaration---for example, omitting the braces around
  1272. an argument or forgetting the \\ in a command name.")
  1273.  
  1274.     ("Missing p-arg in array arg.*" .
  1275. "There is a p that is not followed by an expression in braces in the
  1276. argument of an array or tabular environment, or in the second argument
  1277. of a \\multicolumn command.")
  1278.  
  1279.     ("Missing @-exp in array arg." .
  1280. "There is an @ character not followed by an @-expression in the
  1281. argument of an array or tabular environment, or in the second argument
  1282. of a \\multicolumn command.")
  1283.  
  1284.     ("No such counter." .
  1285. "You have specified a nonexistent counter in a \\setcounter or
  1286. \\addtocounter command. This is probably caused by a simple typing
  1287. error.  However, if the error occurred while a file with the extension
  1288. aux is being read, then you probably used a \\newcounter command
  1289. outside the preamble.")
  1290.  
  1291.     ("Not in outer par mode." .
  1292. "You had a figure or table environment or a \\marginpar command in math
  1293. mode or inside a parbox.")
  1294.  
  1295.     ("\\\\pushtabs and \\\\poptabs don't match." .
  1296. "LaTeX found a \\poptabs with no matching \\pushtabs, or has come to the
  1297. \\end{tabbing} command with one or more unmatched \\pushtabs commands.")
  1298.  
  1299.     ("Something's wrong--perhaps a missing \\\\item." .
  1300. "The most probable cause is an omitted \\item command in a list-making
  1301. environment. It is also caused by forgetting the argument of a
  1302. thebibliography environment.")
  1303.  
  1304.     ("Tab overflow." .
  1305. "A \\= command has exceeded the maximum number of tab stops that LaTeX
  1306. permits.")
  1307.  
  1308.     ("There's no line here to end." .
  1309. "A \\newline or \\\\ command appears between paragraphs, where it makes no
  1310. sense. If you're trying to ``leave a blank line'', use a \\vspace
  1311. command.")
  1312.  
  1313.     ("This may be a LaTeX bug." .
  1314. "LaTeX has become thoroughly confused. This is probably due to a
  1315. previously detected error, but it is possible that you have found an
  1316. error in LaTeX itself. If this is the first error message produced by
  1317. the input file and you can't find anything wrong, save the file and
  1318. contact the person listed in your Local Guide.")
  1319.  
  1320.     ("Too deeply nested." .
  1321. "There are too many list-making environments nested within one another.
  1322. How many levels of nesting are permitted may depend upon what computer
  1323. you are using, but at least four levels are provided, which should be
  1324. enough.")
  1325.  
  1326.     ("Too many unprocessed floats." .
  1327. "While this error can result from having too many \\marginpar commands
  1328. on a page, a more likely cause is forcing LaTeX to save more figures
  1329. and tables than it has room for.  When typesetting its continuous
  1330. scroll, LaTeX saves figures and tables separately and inserts them as
  1331. it cuts off pages. This error occurs when LaTeX finds too many figure
  1332. and/or table environments before it is time to cut off a page, a
  1333. problem that is solved by moving some of the environments farther
  1334. towards the end of the input file. The error can also be caused by a
  1335. ``logjam''---a figure or table that cannot be printed causing others
  1336. to pile up behind it, since LaTeX will not print figures or tables out
  1337. of order. The jam can be started by a figure or table that either is
  1338. too large to fit on a page or won't fit where its optional placement
  1339. argument says it must go. This is likely to happen if the argument
  1340. does not contain a p option.")
  1341.  
  1342.     ("Undefined tab position." .
  1343. "A \\>, \\+, \\-, or \\< command is trying to go to a nonexistent tab
  1344. position---one not defined by a \\= command.")
  1345.  
  1346.     ("\\\\< in mid line." .
  1347. "A \\< command appears in the middle of a line in a tabbing environment.
  1348. This command should come only at the beginning of a line.")
  1349.  
  1350.     ("Counter too large." .
  1351. "Footnotes are being ``numbered'' with letters or footnote symbols and
  1352. LaTeX has run out of letters or symbols. This is probably caused by
  1353. too many \\thanks commands.")
  1354.  
  1355.     ("Double subscript." .
  1356. "There are two subscripts in a row in a mathematical
  1357. formula---something like x_{2}_{3}, which makes no sense.")
  1358.  
  1359.     ("Double superscript." .
  1360. "There are two superscripts in a row in a mathematical
  1361. formula---something like x^{2}^{3}, which makes no sense.")
  1362.  
  1363.     ("Extra alignment tab has been changed to \\\\cr." .
  1364. "There are too many separate items (column entries) in a single row of
  1365. an array or tabular environment. In other words, there were too many &
  1366. 's before the end of the row. You probably forgot the \\\\ at the end of
  1367. the preceding row.")
  1368.  
  1369.     ("Extra \\}, or forgotten \\$." .
  1370. "The braces or math mode delimiters don't match properly. You probably
  1371. forgot a {, \\[, \\(, or $.")
  1372.  
  1373.     ("Font [^ ]* not loaded: Not enough room left." .
  1374. "The document uses more fonts than TeX has room for. If different parts
  1375. of the document use different fonts, then you can get around the
  1376. problem by processing it in parts.")
  1377.  
  1378.     ("I can't find file `.*'." .
  1379. "TeX can't find a file that it needs. If the name of the missing file
  1380. has the extension tex, then it is looking for an input file that you
  1381. specified---either your main file or another file inserted with an
  1382. \\input or \\include command. If the missing file has the extension sty
  1383. , then you have specified a nonexistent document style or style
  1384. option.")
  1385.  
  1386.     ("Illegal parameter number in definition of .*" .
  1387. "This is probably caused by a \\newcommand, \\renewcommand,
  1388. \\newenvironment, or \\renewenvironment command in which a # is used
  1389. incorrectly.  A # character, except as part of the command name \\#,
  1390. can be used only to indicate an argument parameter, as in #2, which
  1391. denotes the second argument. This error is also caused by nesting one
  1392. of the above four commands inside another, or by putting a parameter
  1393. like #2 in the last argument of a \\newenvironment or \\renewenvironment
  1394. command.")
  1395.  
  1396.     ("Illegal unit of measure ([^ ]* inserted)." .
  1397. "If you just got a
  1398.  
  1399.       ! Missing number, treated as zero.
  1400.  
  1401. error, then this is part of the same problem.  If not, it means that
  1402. LaTeX was expecting a length as an argument and found a number
  1403. instead.  The most common cause of this error is writing 0 instead of
  1404. something like 0in for a length of zero, in which case typing return
  1405. should result in correct output. However, the error can also be caused
  1406. by omitting a command argument.")
  1407.  
  1408.     ("Misplaced alignment tab character \\&." .
  1409. "The special character &, which should be used only to separate items
  1410. in an array or tabular environment, appeared in ordinary text. You
  1411. probably meant to type \\&.")
  1412.  
  1413.     ("Missing control sequence inserted." .
  1414. "This is probably caused by a \\newcommand, \\renewcommand, \\newlength,
  1415. or \\newsavebox command whose first argument is not a command name.")
  1416.  
  1417.     ("Missing number, treated as zero." .
  1418. "This is usually caused by a LaTeX command expecting but not finding
  1419. either a number or a length as an argument. You may have omitted an
  1420. argument, or a square bracket in the text may have been mistaken for
  1421. the beginning of an optional argument. This error is also caused by
  1422. putting \\protect in front of either a length command or a command such
  1423. as \\value that produces a number.")
  1424.  
  1425.     ("Missing [{}] inserted." .
  1426. "TeX has become confused. The position indicated by the error locator
  1427. is probably beyond the point where the incorrect input is.")
  1428.  
  1429.     ("Missing \\$ inserted." .
  1430. "TeX probably found a command that can be used only in math mode when
  1431. it wasn't in math mode.  Remember that unless stated otherwise, all
  1432. the commands of Section can be used only in math mode. TeX is not in
  1433. math mode when it begins processing the argument of a box-making
  1434. command, even if that command is inside a math environment. This error
  1435. also occurs if TeX encounters a blank line when it is in math mode.")
  1436.  
  1437.     ("Not a letter." .
  1438. "Something appears in the argument of a \\hyphenation command that
  1439. doesn't belong there.")
  1440.  
  1441.     ("Paragraph ended before [^ ]* was complete." .
  1442. "A blank line occurred in a command argument that shouldn't contain
  1443. one. You probably forgot the right brace at the end of an argument.")
  1444.  
  1445.     ("\\\\[^ ]*font [^ ]* is undefined .*" .
  1446. "These errors occur when an uncommon font is used in math mode---for
  1447. example, if you use a \\sc command in a formula inside a footnote,
  1448. calling for a footnote-sized small caps font.  This problem is solved
  1449. by using a \\load command.")
  1450.  
  1451.     ("Font .* not found." .                                    
  1452. "You requested a family/series/shape/size combination that is totally
  1453. unknown.  There are two cases in which this error can occur:
  1454.   1) You used the \\size macro to select a size that is not available.
  1455.   2) If you did not do that, go to your local `wizard' and
  1456.      complain fiercely that the font selection tables are corrupted!")
  1457.  
  1458.     ("TeX capacity exceeded, sorry .*" .
  1459. "TeX has just run out of space and aborted its execution. Before you
  1460. panic, remember that the least likely cause of this error is TeX not
  1461. having the capacity to process your document.  It was probably an
  1462. error in your input file that caused TeX to run out of room. The
  1463. following discussion explains how to decide whether you've really
  1464. exceeded TeX's capacity and, if so, what to do. If the problem is an
  1465. error in the input, you may have to use the divide and conquer method
  1466. described previously to locate it. LaTeX seldom runs out of space on a
  1467. short input file, so if running it on the last few pages before the
  1468. error indicator's position still produces the error, then there's
  1469. almost certainly something wrong in the input file.
  1470.  
  1471. The end of the error indicator tells what kind of space TeX ran out
  1472. of. The more common ones are listed below, with an explanation of
  1473. their probable causes.
  1474.  
  1475. buffer size 
  1476. ===========
  1477. Can be caused by too long a piece of text as the argument
  1478. of a sectioning, \\caption, \\addcontentsline, or \\addtocontents
  1479. command. This error will probably occur when the \\end{document} is
  1480. being processed, but it could happen when a \\tableofcontents,
  1481. \\listoffigures, or \\listoftables command is executed. To solve this
  1482. problem, use a shorter optional argument. Even if you're producing a
  1483. table of contents or a list of figures or tables, such a long entry
  1484. won't help the reader.
  1485.  
  1486. exception dictionary 
  1487. ====================
  1488. You have used \\hyphenation commands to give TeX
  1489. more hyphenation information than it has room for. Remove some of the
  1490. less frequently used words from the \\hyphenation commands and insert
  1491. \\- commands instead.
  1492.  
  1493. hash size 
  1494. =========
  1495. Your input file defines too many command names and/or uses
  1496. too many cross-ref- erencing labels.
  1497.  
  1498. input stack size 
  1499. ================
  1500. This is probably caused by an error in a command
  1501. definition. For example, the following command makes a circular
  1502. definition, defining \\gnu in terms of itself:
  1503.  
  1504.           \\newcommand{\\gnu}{a \\gnu} % This is wrong!
  1505.  
  1506. When TeX encounters this \\gnu command, it will keep chasing its tail
  1507. trying to figure out what \\gnu should produce, and eventually run out
  1508. of ``input stack''.
  1509.  
  1510. main memory size 
  1511. ================
  1512. This is one kind of space that TeX can run out of when processing a
  1513. short file. There are three ways you can run TeX out of main memory
  1514. space: (1) defining a lot of very long, complicated commands, (2)
  1515. making an index or glossary and having too many \\index or \\glossary
  1516. commands on a single page, and (3) creating so complicated a page of
  1517. output that TeX can't hold all the information needed to generate it.
  1518. The solution to the first two problems is obvious: define fewer
  1519. commands or use fewer \\index and \\glossary commands. The third problem
  1520. is nastier. It can be caused by large tabbing, tabular, array, and
  1521. picture environments. TeX's space may also be filled up with figures
  1522. and tables waiting for a place to go.  To find out if you've really
  1523. exceeded TeX's capacity in this way, put a \\clearpage command in your
  1524. input file right before the place where TeX ran out of room and try
  1525. running it again. If it doesn't run out of room with the \\clearpage
  1526. command there, then you did exceed TeX's capacity.  If it still runs
  1527. out of room, then there's probably an error in your file.  If TeX is
  1528. really out of room, you must give it some help. Remember that TeX
  1529. processes a complete paragraph before deciding whether to cut the
  1530. page. Inserting a \\newpage command in the middle of the paragraph,
  1531. where TeX should break the page, may save the day by letting TeX write
  1532. the current page before processing the rest of the paragraph. (A
  1533. \\pagebreak command won't help.) If the problem is caused by
  1534. accumulated figures and tables, you can try to prevent them from
  1535. accumulating---either by moving them further towards the end of the
  1536. document or by trying to get them to come out sooner.  If you are
  1537. still writing the document, simply add a \\clearpage command and forget
  1538. about the problem until you're ready to produce the final version.
  1539. Changes to the input file are likely to make the problem go away.
  1540.  
  1541. pool size 
  1542. =========
  1543. You probably used too many cross-ref-erencing \\labels and/or defined
  1544. too many new command names. More precisely, the labels and command
  1545. names that you define have too many characters, so this problem can be
  1546. solved by using shorter names. However, the error can also be caused
  1547. by omitting the right brace that ends the argument of either a counter
  1548. command such as \\setcounter, or a \\newenvironment or \\newtheorem
  1549. command.
  1550.  
  1551. save size 
  1552. =========
  1553. This occurs when commands, environments, and the scopes of
  1554. declarations are nested too deeply---for example, by having the
  1555. argument of a \\multiput command contain a picture environment that in
  1556. turn has a \\footnotesize declaration whose scope contains a \\multiput
  1557. command containing a ....")
  1558.  
  1559.     ("Text line contains an invalid character." .
  1560. "The input contains some strange character that it shouldn't. A mistake
  1561. when creating the file probably caused your text editor to insert this
  1562. character. Exactly what could have happened depends upon what text
  1563. editor you used. If examining the input file doesn't reveal the
  1564. offending character, consult the Local Guide for suggestions.")
  1565.  
  1566.     ("Undefined control sequence."   .  
  1567. "TeX encountered an unknown command name. You probably misspelled the
  1568. name. If this message occurs when a LaTeX command is being processed,
  1569. the command is probably in the wrong place---for example, the error
  1570. can be produced by an \\item command that's not inside a list-making
  1571. environment. The error can also be caused by a missing \\documentstyle
  1572. command.")
  1573.  
  1574.     ("Use of [^ ]* doesn't match its definition." .
  1575. "It's probably one of the picture-drawing commands, and you have used
  1576. the wrong syntax for specifying an argument. If it's \\@array that
  1577. doesn't match its definition, then there is something wrong in an
  1578. @-expression in the argument of an array or tabular
  1579. environment---perhaps a fragile command that is not \\protect'ed.")
  1580.  
  1581.     ("You can't use `macro parameter character \\#' in [^ ]* mode." .
  1582. "The special character # has appeared in ordinary text. You probably
  1583. meant to type \\#.")
  1584.  
  1585.     ("Overfull \\\\hbox .*" .
  1586. "Because it couldn't find a good place for a line break, TeX put more
  1587. on this line than it should.")
  1588.  
  1589.     ("Overfull \\\\vbox .*" .
  1590. "Because it couldn't find a good place for a page break, TeX put more
  1591. on the page than it should. ")
  1592.  
  1593.     ("Underfull \\\\hbox .*" .
  1594. "Check your output for extra vertical space.  If you find some, it was
  1595. probably caused by a problem with a \\\\ or \\newline command---for
  1596. example, two \\\\ commands in succession. This warning can also be
  1597. caused by using the sloppypar environment or \\sloppy declaration, or
  1598. by inserting a \\linebreak command.")
  1599.  
  1600.     ("Underfull \\\\vbox .*" .
  1601. "TeX could not find a good place to break the page, so it produced a
  1602. page without enough text on it. ")
  1603.  
  1604. ;; New list items should be placed here 
  1605. ;; 
  1606. ;; ("err-regexp" . "context") 
  1607. ;; 
  1608. ;; the err-regexp item should match anything
  1609.  
  1610.     (".*" . "No help available"))    ; end definition 
  1611. "A list of the form (\"err-regexp\" . \"context\") used by function
  1612. \\{TeX-help-error} to display help-text on an error message or warning.
  1613. err-regexp should be a regular expression matching the error message
  1614. given from TeX/LaTeX, and context should be some lines describing that
  1615. error"
  1616.   :group 'TeX-output
  1617.   :type '(repeat (cons :tag "Entry"
  1618.                (regexp :tag "Match")
  1619.                (string :format "Description:\n%v"))))
  1620.  
  1621. (provide 'tex-buf)
  1622.  
  1623. ;;; tex-buf.el ends here
  1624.